package com.laoyao.controller;

import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.tx.Tx;
import com.laoyao.comm.config.CrossOrigin;
import com.laoyao.comm.model.Type1;
import com.laoyao.comm.model.Type2;
import com.laoyao.service.TypeService;
import com.laoyao.util.TextUtil;

@CrossOrigin
public class VideoTypeController extends Controller {
	private TypeService dao = new TypeService();
    
	//返回一级分类列表
	public void type1List(){
		Integer pageNumber = getParaToInt("pageNumber");
		Integer pageSize = getParaToInt("pageSize");
		
		if(pageNumber==null){
			pageNumber=1;
		}
		if(pageSize==null){
			pageSize=10;
		}
		
		renderJson("page",TypeService.me.type1List(pageNumber, pageSize));
	}
	//一级分类添加
	public void addType1(){
		Type1 t1 = getBean(Type1.class,"");
		
		if(TextUtil.isEmpty(t1.getTypeName())){
			renderJson("msg","分类名不能为空！");
			return;
		}

		if(dao.isExistsType1(t1.getTypeName())){
			setAttr("msg","分类已存在");
			renderJson();
			return;
		}
		
		
		setAttr("success",t1.save());
		setAttr("type1",t1);
		renderJson();
	}
	//删除一级分类
	@Before(Tx.class)
	public void delType1(){
		Type1 t1 = getBean(Type1.class,"");
		TypeService.me.delByType1Id(t1.getId());
		
		if(!TypeService.me.isEnableDelType1(t1.getId())){
			setAttr("msg","有视频引用该类型");
			renderJson();
			return;
		}
		
		renderJson("success",t1.delete());
	}
	//获取type1数据
	public void getType1ById(){
		String id =  getPara("id");
		renderJson("type1",Type1.dao.findById(id));
	}
	//修改一级分类
	public void updateType1(){
		Type1 t1 = getBean(Type1.class,"");
		if(TextUtil.isEmpty(t1.getTypeName())){
			renderJson("msg","分类名不能为空！");
			return;
		}

		renderJson("success",t1.update());
	}
	
	//获取二级分类列表
	public void getType2List(){
		Integer pageNumber = getParaToInt("pageNumber");
		Integer pageSize = getParaToInt("pageSize");
		Integer id = getParaToInt("id");
		
		if(pageNumber==null){
			pageNumber=1;
		}
		if(pageSize==null){
			pageSize=10;
		}
		
		renderJson("page",TypeService.me.type2List(pageNumber, pageSize,id));
	}
	
	//获取type2
	public void getType2ById(){
		renderJson("type2",Type2.dao.findById(getPara("id")));
	}
	//增加二级分类
	public void addType2(){
		Type2 t2 = getBean(Type2.class,"");
		t2.setType1Id(getParaToInt("type1Id"));
		
		if(TextUtil.isEmpty(t2.getTypeName())){
			renderJson("msg","分类名不能为空！");
			return;
		}
		
		if(dao.isExistsType2(t2.getTypeName(),t2.getType1Id())){
			setAttr("msg","分类已存在");
			renderJson();
			return;
		}
		setAttr("success",t2.save());
		setAttr("type2",t2);
		renderJson();
	}
	//删除二级分类
	public void delType2(){
		Type2 t2 = getBean(Type2.class,"");
		
		if(!TypeService.me.isEnableDelType2(t2.getId())){
			setAttr("msg","有视频引用该类型");
			renderJson();
			return;
		}
		
		renderJson("success",t2.delete());
	}
	//修改二级分类
	public void updateType2(){
		Type2 t2 = getBean(Type2.class,"");
		if(TextUtil.isEmpty(t2.getTypeName())){
			renderJson("msg","分类名不能为空！");
			return;
		}
		renderJson("success",t2.update());
	}

}
